#! /bin/sh

testdir="testdir.lrzsz"
srcdir="$1"
if test $srcdir = . ; then
	srcdir=`pwd`
fi
if test $srcdir = .. ; then
	srcdir=`pwd`/..
fi
objdir="$2"
if test $objdir = . ; then
	objdir=`pwd`
fi

if test "x$3" = x ; then
	mode=all
else
	mode=$3
fi

#SZBUFSIZE="-B 524288"
RZBUFSIZE="-B 524288"

SZ="$objdir/src/lsz"
RZ="$objdir/src/lrz"

echo checking with srcdir = $1 and objdir = $2

# z_test_files: files to test zmodem with.
# z8_test_files: files to test zmodem-8k with. Optional.
# y_test_files: files to test ymodem with.
# x_test_files: files to test xmodem with. Must be ascii, as we need the
#               ascii mode to remove the padding ^Zs.

# generate list of testfiles 
z_test_files=""
for i in $srcdir/src/l?z.c ; do
	z_test_files="$z_test_files $i" 
	x_test_files="$x_test_files $i" 
done
for i in $objdir/src/l?z ; do
	z_test_files="$z_test_files $i" 
	y_test_files="$y_test_files $i" 
done
rm -f $objdir/null1 $objdir/null2
touch $objdir/null1 $objdir/null2
x_test_files="$objdir/null1 $x_test_files" 
y_test_files="$objdir/null1 $y_test_files" 
z_test_files="$objdir/null1 $objdir/null2 $z_test_files" 
for i in /pub/gnu/emacs/emacs-2*.tar.gz ; do
	if test -f "$i" ; then
		z8_test_files="$z8_test_files $i" 
		z_test_files="$z_test_files $i" 
	fi
done

# change to tmp dir
if test "x$TMPDIR" = x ; then
	cd /tmp
else
	cd $TMPDIR || cd /tmp
fi

rm -rf $testdir
mkdir $testdir
exec 5>$testdir/error.log
(mkfifo $testdir/pipe || mknod $testdir/pipe p) 2>&5

# test zmodem
if test "$mode" = all -o "$mode" = z ; then
	echo "testing ZMODEM"
	mkdir $testdir/zmodem
	failed=0
	($SZ -q $SZBUFSIZE $z_test_files ) <$testdir/pipe | \
		(cd $testdir/zmodem ; echo "123" >null1 ; exec $RZ -y $RZBUFSIZE $QUIET >>../pipe )
	for i in $z_test_files ; do 
		bn=`basename $i`
		cmp $i $testdir/zmodem/$bn
		if test $? -eq 0 ; then
			rm -f $testdir/zmodem/$bn
		else
			failed=1
		fi
	done
	rmdir $testdir/zmodem 2>&5
	if test $failed = 1 ; then 
		echo "ZMODEM test failed"
		any_failure="$any_failure ZMODEM"
	fi
fi
# test zmodem framlen
if test "$mode" = f ; then
	echo "testing ZMODEMframlen"
	mkdir $testdir/zmodem
	failed=0
	($SZ -q $SZBUFSIZE -l 128 $z_test_files ) <$testdir/pipe | \
		(cd $testdir/zmodem ; exec $RZ $RZBUFSIZE $QUIET >>../pipe )
	for i in $z_test_files ; do 
		bn=`basename $i`
		cmp $i $testdir/zmodem/$bn
		if test $? -eq 0 ; then
			rm -f $testdir/zmodem/$bn
		else
			failed=1
		fi
	done
	rmdir $testdir/zmodem 2>&5
	if test $failed = 1 ; then 
		echo "ZMODEM test failed"
		any_failure="$any_failure ZMODEM"
	fi
fi

# test zmodem-4k
if test "$mode" = all -o "$mode" = z4 ; then
	echo "testing ZMODEM4K"
	mkdir $testdir/zmodem4k
	failed=0
	($SZ -q4 $SZBUFSIZE $z_test_files ) <$testdir/pipe | \
		(cd $testdir/zmodem4k ; exec $RZ $RZBUFSIZE $QUIET >>../pipe )
	for i in $z_test_files ; do 
		bn=`basename $i`
		cmp $i $testdir/zmodem4k/$bn
		if test $? -eq 0 ; then
			rm -f $testdir/zmodem4k/$bn
		else
			failed=1
		fi
	done
	rmdir $testdir/zmodem4k 2>&5
	if test $failed = 1 ; then 
		echo "ZMODEM4k test failed"
		any_failure="$any_failure ZMODEM4k"
	fi
fi

# test zmodem8k
if test "$mode" = all -o "$mode" = z8 ; then
	# we test this only if we have something really large, because this is
	# we only case we can expect any differences
	if test "x$z8_test_files" = x ; then
		:
	else
		echo "testing ZMODEM8K"
		mkdir $testdir/zmodem8k
		failed=0
		$SZ -q8OT $SZBUFSIZE $z8_test_files <$testdir/pipe | \
			(cd $testdir/zmodem8k ; $RZ -O $RZBUFSIZE $QUIET >>../pipe )
		for i in $z8_test_files ; do 
			bn=`basename $i`
			cmp $i $testdir/zmodem8k/$bn
			if test $? -eq 0 ; then
				rm -f $testdir/zmodem8k/$bn
			else
				failed=1
			fi
		done
		if test $failed = 1 ; then 
			echo "ZMODEM8k test failed"
			any_failure="$any_failure ZMODEM8k"
		fi
		rmdir $testdir/zmodem8k 2>&5
	fi
fi

if test "$mode" = all -o "$mode" = z8N ; then
	# we test this only if we have something really large, because this is
	# we only case we can expect any differences
	if test "x$z8_test_files" = x ; then
		:
	else
		echo "testing ZMODEM8K-NULL"
		mkdir $testdir/zmodem8k-NULL
		$SZ -q8OT $SZBUFSIZE $z8_test_files <$testdir/pipe | \
			(cd $testdir/zmodem8k-NULL ; $RZ -DO $RZBUFSIZE $QUIET >>../pipe )
		rmdir $testdir/zmodem8k-NULL 2>&5
	fi
fi

if test "$mode" = error ; then
	# we test this only if we have something really large, because this is
	# we only case we can expect any differences
	if test "x$y_test_files" = x ; then
		:
	else
		echo "testing error, lsz"
		mkdir $testdir/error.lsz
		failed=0
		# strace -vs 128 -o out $SZ -8 -vvvvvvvv $SZBUFSIZE $y_test_files <$testdir/pipe | \
		$SZ -8q $SZBUFSIZE $y_test_files <$testdir/pipe | \
			(cd $testdir/error.lsz ; $RZ --errors 32768 $RZBUFSIZE $QUIET >>../pipe 2>/dev/null)
		for i in $y_test_files ; do 
			bn=`basename $i`
			cmp $i $testdir/error.lsz/$bn
			if test $? -eq 0 ; then
				rm -f $testdir/error.lsz/$bn
			else
				failed=1
			fi
		done
		if test $failed = 1 ; then 
			echo "error test failed"
			any_failure="$any_failure error.lsz"
		fi
		rmdir $testdir/error.lsz 2>&5

		# omen zmodem has a really bad error behaviour. it gets so slow
		# that you wouldn't believe it ...
		echo "testing error, omen.sz"
		mkdir $testdir/error.omen
		failed=0
		omen.sz $y_test_files <$testdir/pipe | \
			(cd $testdir/error.omen ; $RZ -vvv --errors 32768 $RZBUFSIZE $QUIET >>../pipe 2>/tmp/out2)
		for i in $y_test_files ; do 
			bn=`basename $i`
			cmp $i $testdir/error.omen/$bn
			if test $? -eq 0 ; then
				rm -f $testdir/error.omen/$bn
			else
				failed=1
			fi
		done
		if test $failed = 1 ; then 
			echo "error.omen test failed"
			any_failure="$any_failure error.omen"
		fi
		rmdir $testdir/error.omen 2>&5

		echo "testing error, zm.sz"
		mkdir $testdir/error.zm
		failed=0
		zmtx $y_test_files <$testdir/pipe | \
			(cd $testdir/error.zm ; $RZ -vvv --errors 32768 $RZBUFSIZE $QUIET >>../pipe 2>/tmp/out2)
		for i in $y_test_files ; do 
			bn=`basename $i`
			cmp $i $testdir/error.zm/$bn
			if test $? -eq 0 ; then
				rm -f $testdir/error.zm/$bn
			else
				failed=1
			fi
		done
		if test $failed = 1 ; then 
			echo "error.zm test failed"
			any_failure="$any_failure error.zm"
		fi
		rmdir $testdir/error.zm 2>&5
	fi
fi


# test ymodem
if test "$mode" = all -o "$mode" = y ; then
	echo "testing YMODEM"
	mkdir $testdir/ymodem
	failed=0
	$SZ --ymodem -q $y_test_files -vvvvv <$testdir/pipe | \
		(cd $testdir/ymodem ; $RZ $QUIET --ymodem >>../pipe)
	for i in $y_test_files ; do 
		bn=`basename $i`
		cmp $i $testdir/ymodem/$bn
		if test $? -eq 0 ; then
			rm -f $testdir/ymodem/$bn
		else
			failed=1
		fi
	done
	rmdir $testdir/ymodem 2>&5
	if test $failed = 1 ; then 
		echo "YMODEM test failed"
		any_failure="$any_failure YMODEM"
	fi
fi

if test "$mode" = all -o "$mode" = x ; then
	echo "testing XMODEM"
	mkdir $testdir/xmodem
	failed=0
	for i in $x_test_files; do
		bn=`basename $i`
		$SZ --xmodem -qa $i <$testdir/pipe | \
			(cd $testdir/xmodem ; $RZ $QUIET -a --xmodem $bn >>../pipe)
		cmp $i $testdir/xmodem/$bn
		if test $? -eq 0 ; then
			rm -f $testdir/xmodem/$bn
		else
			failed=1
		fi
	done
	rmdir $testdir/xmodem 2>&5
	if test $failed = 1 ; then 
		echo "XMODEM test failed"
		any_failure="$any_failure XMODEM"
	fi
fi

if test "$mode" = all -o "$mode" = manage ; then
	echo "testing file management, appending"
	mkdir $testdir/manag.append
	failed=0
	$SZ -q --append $srcdir/src/lsz.c $srcdir/src/lsz.c <$testdir/pipe | \
		(cd $testdir/manag.append ; $RZ $QUIET >>../pipe)
	rm -f $testdir/manag.append/compare
	cp $srcdir/src/lsz.c $testdir/manag.append/compare
	cat $srcdir/src/lsz.c >>$testdir/manag.append/compare
	cmp $testdir/manag.append/compare $testdir/manag.append/lsz.c
	if test $? -eq 0 ; then
		rm -f $testdir/manag.append/lsz.c
		rm -f $testdir/manag.append/compare
	else
		failed=1
	fi
	rmdir $testdir/manag.append 2>&5
	if test $failed = 1 ; then 
		echo "management/append test failed"
		any_failure="$any_failure manag.append"
	fi

	echo "testing file management, renaming"
	mkdir $testdir/manag.rename
	failed=0
	rm -f $testdir/manag.rename/lsz.c
	cp $srcdir/src/lsz.c $testdir/manag.rename/
	$SZ -q --rename $srcdir/src/lsz.c $srcdir/src/lsz.c <$testdir/pipe | \
		(cd $testdir/manag.rename ; $RZ $QUIET >>../pipe)
	cmp $testdir/manag.rename/lsz.c $srcdir/src/lsz.c
	if test $? -ne 0 ; then
		failed=1
	else
		cmp $testdir/manag.rename/lsz.c.0 $srcdir/src/lsz.c
		if test $? -ne 0 ; then
			failed=1	
		else
			cmp $testdir/manag.rename/lsz.c.1 $srcdir/src/lsz.c
			if test $? -ne 0 ; then
				failed=1	
			fi
		fi
	fi
	if test $failed -eq 0 ; then
		rm -f $testdir/manag.rename/lsz.c
		rm -f $testdir/manag.rename/lsz.c.0
		rm -f $testdir/manag.rename/lsz.c.1
	fi
	rmdir $testdir/manag.rename 2>&5
	if test $failed = 1 ; then 
		echo "management/rename test failed"
		any_failure="$any_failure manag.rename"
	fi
fi


# resume -r 
if test "$mode" = all -o "$mode" = sz-r ; then
	mkdir $testdir/resume-s
	echo "testing ZMODEM resume (sz -r)"
	dd if=$srcdir/src/Makefile.in of=$testdir/resume-s/lsz.c bs=256 count=5 2>&5
	($SZ -rq $srcdir/src/lsz.c) <$testdir/pipe | \
		(cd $testdir/resume-s ; $RZ $QUIET >>../pipe )
	echo "differences are normal:" >&5
	if cmp $srcdir/src/lsz.c $testdir/resume-s/lsz.c >&5 ; then
		echo "receiver did not resume, but file is ok"
		any_failure="$any_failure ZMODEM-resume/sz"
	else
		dd if=$srcdir/src/lsz.c of=$testdir/resume-s/lsz.c1 bs=256 count=5 2>&5
		dd if=$testdir/resume-s/lsz.c bs=256 skip=5 >>$testdir/resume-s/lsz.c1 2>&5
		if cmp $srcdir/src/lsz.c $testdir/resume-s/lsz.c1 ; then
			:
			rm -f $testdir/resume-s/lsz.c
			rm -f $testdir/resume-s/lsz.c1
			rmdir $testdir/resume-s 2>&5
		else
			echo "sender resumed, but files differ. This is really bad"
			any_failure="$any_failure ZMODEM-resume/sz"
		fi
	fi
fi

if test "$mode" = all -o "$mode" = rz-r ; then
    echo "testing ZMODEM resume (rz -r)"
	mkdir $testdir/resume-r 2>&5
	dd if=$srcdir/src/Makefile.in of=$testdir/resume-r/lsz.c bs=256 count=5 2>&5
	($SZ -q $srcdir/src/lsz.c) <$testdir/pipe | \
		(cd $testdir/resume-r ; $RZ -r $QUIET >>../pipe )
	echo "differences are normal:" >&5
	if cmp $srcdir/src/lsz.c $testdir/resume-r/lsz.c >&5 ; then
		echo "receiver did not resume, but file is ok"
		any_failure="$any_failure ZMODEM-resume/rz"
	else
		dd if=$srcdir/src/lsz.c of=$testdir/resume-r/lsz.c1 bs=256 count=5 2>&5
		dd if=$testdir/resume-r/lsz.c bs=256 skip=5 >>$testdir/resume-r/lsz.c1 2>&5
		if cmp $srcdir/src/lsz.c $testdir/resume-r/lsz.c1 ; then
			:
			rm -f $testdir/resume-r/lsz.c
			rm -f $testdir/resume-r/lsz.c1
			rmdir $testdir/resume-r 2>&5
		else
			echo "sender resumed, but files differ. This is really bad"
			any_failure="$any_failure ZMODEM-resume/rz"
		fi
	fi
fi

# test lsz -> original rz
if test "$mode" = orig-rz ; then
	echo "testing ZMODEM"
	mkdir $testdir/zmodem
	failed=0
	($SZ -q  $SZBUFSIZE $z_test_files ) 2>/dev/null <$testdir/pipe | \
		(cd $testdir/zmodem ; exec omen.rz $QUIET >>../pipe)
	for i in $z_test_files ; do 
		bn=`basename $i`
		cmp $i $testdir/zmodem/$bn
		if test $? -eq 0 ; then
			rm -f $testdir/zmodem/$bn
		else
			failed=1
		fi
	done
	rmdir $testdir/zmodem 2>&5
	if test $failed = 1 ; then 
		echo "orig-rz test failed"
		any_failure="$any_failure orig-rz"
	fi
fi


# test original sz -> lrz
if test "$mode" = orig-sz ; then
	echo "testing ZMODEM"
	mkdir $testdir/zmodem
	failed=0
	(omen.sz $z_test_files ) 2>/dev/null <$testdir/pipe | \
		(cd $testdir/zmodem ; exec $RZ >>../pipe)
	for i in $z_test_files ; do 
		bn=`basename $i`
		cmp $i $testdir/zmodem/$bn
		if test $? -eq 0 ; then
			rm -f $testdir/zmodem/$bn
		else
			failed=1
		fi
	done
	rmdir $testdir/zmodem 2>&5
	if test $failed = 1 ; then 
		echo "orig-sz test failed"
		any_failure="$any_failure orig-sz"
	fi
fi

# test original sz -> lrz
if test "$mode" = orig-kombi ; then
	echo "testing ZMODEM"
	mkdir $testdir/zmodem
	failed=0
	(omen.sz $y_test_files ; 
			sleep 1; 
			cd $testdir/zmodem; 
			echo 
			exec omen.rz ) 2>/dev/null <$testdir/pipe | \
		(cd $testdir/zmodem ; 
			$RZ ; 
			rm -f * ; 
			sleep 3 ;
			$SZ $y_test_files ) >>$testdir/pipe
	for i in $y_test_files ; do 
		bn=`basename $i`
		cmp $i $testdir/zmodem/$bn
		if test $? -eq 0 ; then
			rm -f $testdir/zmodem/$bn
		else
			failed=1
		fi
	done
	rmdir $testdir/zmodem 2>&5
	if test $failed = 1 ; then 
		echo "orig-kombi test failed"
		any_failure="$any_failure orig-kombi"
	fi
fi

# test lsz -> zm rz (zxrx)
if test "$mode" = zm-rz ; then
	echo "testing ZMODEM"
	mkdir $testdir/zmodem
	failed=0
	($SZ -q  $SZBUFSIZE $z_test_files ) 2>/dev/null <$testdir/pipe | \
		(cd $testdir/zmodem ; exec zmrx $QUIET >>../pipe)
	for i in $z_test_files ; do 
		bn=`basename $i`
		cmp $i $testdir/zmodem/$bn
		if test $? -eq 0 ; then
			rm -f $testdir/zmodem/$bn
		else
			failed=1
		fi
	done
	rmdir $testdir/zmodem 2>&5
	if test $failed = 1 ; then 
		echo "orig-rz test failed"
		any_failure="$any_failure zm-rz"
	fi
fi

# test zm sz (zmtx) -> lrz
if test "$mode" = zm-sz ; then
	echo "testing ZMODEM"
	mkdir $testdir/zmodem
	failed=0
	(zmtx $z_test_files ) 2>/dev/null <$testdir/pipe | \
		(cd $testdir/zmodem ; exec $RZ >>../pipe)
	for i in $z_test_files ; do 
		bn=`basename $i`
		cmp $i $testdir/zmodem/$bn
		if test $? -eq 0 ; then
			rm -f $testdir/zmodem/$bn
		else
			failed=1
		fi
	done
	rmdir $testdir/zmodem 2>&5
	if test $failed = 1 ; then 
		echo "orig-sz test failed"
		any_failure="$any_failure zm-sz"
	fi
fi

# test turbo mode lsz -> zmrx, omen.rz, lrz
if test "$mode" = turbo ; then
	echo "testing turbo -> zmrx"
	mkdir $testdir/turbo.zmrx
	failed=0
	($SZ -qT $SZBUFSIZE $z_test_files ) 2>/dev/null <$testdir/pipe | \
		(cd $testdir/turbo.zmrx ; exec zmrx >>../pipe)
	for i in $z_test_files ; do 
		bn=`basename $i`
		cmp $i $testdir/turbo.zmrx/$bn
		if test $? -eq 0 ; then
			rm -f $testdir/turbo.zmrx/$bn
		else
			failed=1
		fi
	done
	rmdir $testdir/turbo.zmrx 2>&5
	if test $failed = 1 ; then 
		echo "turbo-zmrx test failed"
		any_failure="$any_failure turbo-zmrx"
	fi

	echo "testing turbo -> omen.rz"
	mkdir $testdir/turbo.omen.rz
	failed=0
	($SZ -qT $SZBUFSIZE $z_test_files ) 2>/dev/null <$testdir/pipe | \
		(cd $testdir/turbo.omen.rz ; exec omen.rz >>../pipe)
	for i in $z_test_files ; do 
		bn=`basename $i`
		cmp $i $testdir/turbo.omen.rz/$bn
		if test $? -eq 0 ; then
			rm -f $testdir/turbo.omen.rz/$bn
		else
			failed=1
		fi
	done
	rmdir $testdir/turbo.omen.rz 2>&5
	if test $failed = 1 ; then 
		echo "turbo-omen.rz test failed"
		any_failure="$any_failure turbo-omen.rz"
	fi

	echo "testing turbo -> lrz"
	mkdir $testdir/turbo.lrz
	failed=0
	($SZ -qT $SZBUFSIZE $z_test_files ) 2>/dev/null <$testdir/pipe | \
		(cd $testdir/turbo.lrz ; exec lrz >>../pipe)
	for i in $z_test_files ; do 
		bn=`basename $i`
		cmp $i $testdir/turbo.lrz/$bn
		if test $? -eq 0 ; then
			rm -f $testdir/turbo.lrz/$bn
		else
			failed=1
		fi
	done
	rmdir $testdir/turbo.omen.rz 2>&5
	if test $failed = 1 ; then 
		echo "turbo-lrz test failed"
		any_failure="$any_failure turbo-lrz"
	fi
fi

#
# startup timing checks
#
# test lsz -> zm rz (zxrx)
if test "$mode" = startup ; then

	echo "testing lsz -> zm rz. lsz sleeps 10 seconds"
	mkdir $testdir/startup.1
	failed=0
	($SZ --delay-startup 10 -q  $SZBUFSIZE $z_test_files ) 2>/dev/null <$testdir/pipe | \
		(cd $testdir/startup.1 ; exec zmrx $QUIET >>../pipe)
	for i in $z_test_files ; do 
		bn=`basename $i`
		cmp $i $testdir/startup.1/$bn
		if test $? -eq 0 ; then
			rm -f $testdir/startup.1/$bn
		else
			failed=1
		fi
	done
	rmdir $testdir/startup.1 2>&5
	if test $failed = 1 ; then 
		echo "zm-rz startup-timing test failed"
		any_failure="$any_failure zm-rz-startup-timing"
	fi

	echo "testing lsz -> omen.rz. lsz sleeps 10 seconds"
	mkdir $testdir/startup.2
	failed=0
	($SZ --delay-startup 10 -q  $SZBUFSIZE $z_test_files ) 2>/dev/null <$testdir/pipe | \
		(cd $testdir/startup.2 ; exec omen.rz $QUIET >>../pipe)
	for i in $z_test_files ; do 
		bn=`basename $i`
		cmp $i $testdir/startup.2/$bn
		if test $? -eq 0 ; then
			rm -f $testdir/startup.2/$bn
		else
			failed=1
		fi
	done
	rmdir $testdir/startup.2 2>&5
	if test $failed = 1 ; then 
		echo "omen.rz startup-timing test failed"
		any_failure="$any_failure omen-rz-startup-timing"
	fi

	echo "testing lsz -> lrz. lsz sleeps 10 seconds"
	mkdir $testdir/startup.3
	failed=0
	($SZ --delay-startup 10 -q  $SZBUFSIZE $z_test_files ) 2>/dev/null <$testdir/pipe | \
		(cd $testdir/startup.3 ; exec $RZ $QUIET >>../pipe)
	for i in $z_test_files ; do 
		bn=`basename $i`
		cmp $i $testdir/startup.3/$bn
		if test $? -eq 0 ; then
			rm -f $testdir/startup.3/$bn
		else
			failed=1
		fi
	done
	rmdir $testdir/startup.3 2>&5
	if test $failed = 1 ; then 
		echo "lrz startup-timing test failed"
		any_failure="$any_failure lrz-startup-timing"
	fi

	echo "testing zmtx -> lrz. lrz sleeps 10 seconds"
	mkdir $testdir/startup.4
	failed=0
	(zmtx $z_test_files ) 2>/dev/null <$testdir/pipe | \
		(cd $testdir/startup.4 ; exec $RZ --delay-startup 10 $QUIET >>../pipe)
	for i in $z_test_files ; do 
		bn=`basename $i`
		cmp $i $testdir/startup.4/$bn
		if test $? -eq 0 ; then
			rm -f $testdir/startup.4/$bn
		else
			failed=1
		fi
	done
	rmdir $testdir/startup.4 2>&5
	if test $failed = 1 ; then 
		echo "zmtx startup-timing test failed"
		any_failure="$any_failure zmtx-startup-timing"
	fi

	echo "testing omen.sz -> lrz. lrz sleeps 10 seconds"
	mkdir $testdir/startup.5
	failed=0
	(omen.sz $z_test_files ) 2>/dev/null <$testdir/pipe | \
		(cd $testdir/startup.5 ; exec $RZ --delay-startup 10 $QUIET >>../pipe)
	for i in $z_test_files ; do 
		bn=`basename $i`
		cmp $i $testdir/startup.5/$bn
		if test $? -eq 0 ; then
			rm -f $testdir/startup.5/$bn
		else
			failed=1
		fi
	done
	rmdir $testdir/startup.5 2>&5
	if test $failed = 1 ; then 
		echo "omen.sz startup-timing test failed"
		any_failure="$any_failure omen.sz-startup-timing"
	fi

	echo "testing lsz -> lrz. lrz sleeps 10 seconds"
	mkdir $testdir/startup.6
	failed=0
	($SZ -q $z_test_files ) 2>/dev/null <$testdir/pipe | \
		(cd $testdir/startup.6 ; exec $RZ --delay-startup 10 $QUIET >>../pipe)
	for i in $z_test_files ; do 
		bn=`basename $i`
		cmp $i $testdir/startup.6/$bn
		if test $? -eq 0 ; then
			rm -f $testdir/startup.6/$bn
		else
			failed=1
		fi
	done
	rmdir $testdir/startup.6 2>&5
	if test $failed = 1 ; then 
		echo "lsz startup-timing test failed"
		any_failure="$any_failure lsz-startup-timing"
	fi
fi

if test "$mode" = stop-at ; then
	echo "testing lsz -> lrz. lsz stops after 10 seconds"
	mkdir $testdir/stop-at
	failed=0
	($SZ --stop-at +10 $z8_test_files ) 2>/dev/null <$testdir/pipe | \
		(cd $testdir/stop-at ; exec $RZ $QUIET >>../pipe)
	for i in $z8_test_files ; do 
		bn=`basename $i`
		cmp $i $testdir/stop-at/$bn
		if test $? -eq 0 ; then
			rm -f $testdir/stop-at/$bn
		else
			failed=1
		fi
	done
	rmdir $testdir/stop-at 2>&5
	if test $failed = 1 ; then 
		echo "stop-at test failed"
		any_failure="$any_failure stop-at"
	fi
fi

if test "x$any_failure" = x  ; then
	:
else
	echo "the following tests failed:"
	echo $any_failure
	echo "have a look into `pwd`/$testdir"
	exit 1
fi

echo "All tests OK."
if test "$mode" = all -o "$mode" = abuse ; then
	echo
	echo "going to tests misuse. Errors are ok, hangs are not!"

	echo
	echo "both sides sending:"
	$SZ -q $z_test_files <$testdir/pipe | \
		($SZ $z_test_files >>$testdir/pipe )

	echo
	echo "both sides receiving:"
	$RZ -q <$testdir/pipe | \
		( $RZ >>$testdir/pipe )
fi

rm -rf $testdir
rm -f $objdir/null1 $objdir/null2
exit 0

